/*
 * @Descripttion:
 * @version:
 * @Author: 麦冬果果
 * @Date: 2021-07-15 08:53:34
 * @LastEditors: 麦冬果果
 * @LastEditTime: 2021-09-07 11:04:51
 */
package hook

import (
	"fastapi/src/app/define"

	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

var Index = index{}

type index struct{}

//BEFORE_SERVE
func (h *index) BeforeServer(r *ghttp.Request) {
	//设置接口值
	defaltHook := GetHookByName("getApi")
	defaltHook.Run(r)
	//验证参数

	apis := r.GetCtxVar("appApi")
	var appApi *define.AppApi
	gconv.Struct(apis, &appApi)

	if appApi.HookWay == "BEFORE_SERVE" {
		hook := GetHookByName(appApi.HookName)
		hook.Run(r)
	}
}

//AFTER_SERVE
func (h *index) AfterServer(r *ghttp.Request) {
	apis := r.GetCtxVar("appApi")
	var appApi *define.AppApi
	gconv.Struct(apis, &appApi)
	if appApi.HookWay == "AFTER_SERVE" {
		hook := GetHookByName(appApi.HookName)
		hook.Run(r)
	}
}

//BEFORE_OUTPUT
func (h *index) BeforeOutput(r *ghttp.Request) {
	apis := r.GetCtxVar("appApi")
	var appApi *define.AppApi
	gconv.Struct(apis, &appApi)

	if appApi.HookWay == "BEFORE_OUTPUT" {
		hook := GetHookByName(appApi.HookName)
		hook.Run(r)
	}
}

//AFTER_OUTPUT
func (h *index) AfterOutput(r *ghttp.Request) {
	apis := r.GetCtxVar("appApi")
	var appApi *define.AppApi
	gconv.Struct(apis, &appApi)

	if appApi.HookWay == "AFTER_OUTPUT" {
		hook := GetHookByName(appApi.HookName)
		hook.Run(r)
	}
}
